// 分仓库 headerReducer

// 引用actionType规范
import {
    SearchGetFocus,
    SearchLostFocus,
    ChangeList,
    SearchMouseEnter,
    SearchMouseLeave,
    NextTips,
    CloseSpin
} from './actionTypes';
// 引用immutable对象
import { fromJS } from "immutable";


const defaultState = fromJS ({
    list: [],          // 热门搜索数组
    focused: false,    // 搜索输入框是否聚焦
    totalPage: 1,      // 总页
    page: 1,           // 起始页
    mouseIn: false,    // 鼠标移入
    spinStatus: false, // 选装
});
export default (state = defaultState, action) => {
    switch (action.type) {
        case SearchGetFocus:
            return state.set('focused', true);
        case SearchLostFocus:
            return state.set('focused', false);
        case ChangeList:
            return state.set('list', action.data).set('totalPage', action.totalPage);
        case SearchMouseEnter:
            return state.set('mouseIn', true);
        case SearchMouseLeave:
            return state.set('mouseIn', false);
        case NextTips:
            const totalPage = state.get('totalPage');
            let page = state.get('page');
            if(page < totalPage){
                page ++
            }else {
                page = 1;
            }
            return state.merge({
                page: fromJS(page),
                spinStatus: fromJS(true)
            });
        case CloseSpin:
            return state.set('spinStatus', false);
        default:
            return state;
    }
}
